######################
## General settings ##
######################

## color scheme
rhg_cols <-
  c("#2166ac",
    "#b2182b",
    "#67a9cf",
    "#ef8a62",
    "#d1e5f0",
    "#fddbc7")
## rhg_cols <- c("#d7191c", "#2c7bb6", "#fdae61", "#abd9e9")



if (paper == TRUE) {
  dir.fig <- dir.fig.paper
  width.single <- 4
  height.single <- 3
  
  width.double <- 2
  height.double <- 2
  
  width.double2 <- 3
  height.double2 <- 2.5
  
  width.third <- 2
  height.third <- 2
  
  git.footnote.val <- TRUE
  cbPalette <-
    c(
      "#999999",
      "#E69F00",
      "#56B4E9",
      "#009E73",
      "#F0E442",
      "#0072B2",
      "#D55E00",
      "#CC79A7"
    )
  cbPalette <- c("#d7191c", "#2c7bb6", "#fdae61", "#abd9e9")
  cbPalette <-
    c('#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99')
  ## cbPalette <- c("#66c2a5", "#fc8d62", "#8da0cb")
  pythonPalette <- c("#1f77b4", "#ff7f0e", "#2ca02c")
  matlabPalette <- c("#000000", "#FF0000", "#0000FF", "#FF6600")
  matlabPaletteReverse <- c("#0000FF", "#FF0000", "#000000")
  redBluePalette <- c("#FF0000", "#0000FF")
  
  # https://personal.sron.nl/~pault/
  paulTolVibrantPalette <-
    c(
      '#EE7733',
      '#0077BB',
      '#33BBEE',
      '#EE3377',
      '#CC3311',
      '#009988',
      '#BBBBBB',
      '#000000'
    )
  
  
  
  gray_black <- c('gray', 'black')
  black_gray <- c('black', 'gray')
  fig.theme.deciles <-
    list(
      theme_bw(),
      scale_colour_manual(values = gray_black),
      scale_fill_manual(values = gray_black),
      theme(
        legend.title = element_text(family = "serif"),
        legend.text = element_text(family = "serif"),
        axis.title.y = element_text(family = "serif"),
        axis.title.x = element_text(family = "serif"),
        axis.text.x = element_text(family = "serif"),
        axis.text.y = element_text(family = "serif"),
        plot.title = element_text(family = "serif", hjust = 0.5)
      )
    )
  fig.theme.black_gray <-
    list(
      theme_bw(),
      scale_colour_manual(values = black_gray),
      scale_fill_manual(values = black_gray)
    )
  
  myPalette <- matlabPalette
  
  fig.theme <-
    list(
      theme_bw(),
      scale_colour_manual(values = myPalette),
      scale_fill_manual(values = myPalette)
    )
  
  theme.serif <- theme(
    legend.title = element_text(family = "serif"),
    legend.text = element_text(family = "serif"),
    axis.title.y = element_text(family = "serif", margin = margin(
      t = 0,
      r = 5,
      b = 0,
      l = 0
    )),
    axis.title.x = element_text(family = "serif"),
    axis.text.x = element_text(family = "serif"),
    axis.text.y = element_text(family = "serif"),
    plot.title = element_text(
      family = "serif",
      hjust = 0.5,
      size = 11
    )
  )
  
  col.M <- myPalette[[1]]
  col.R <- myPalette[[2]]
  col.C <- myPalette[[3]]
  col.four <- paulTolVibrantPalette[[4]]
  col.five <- paulTolVibrantPalette[[5]]
  
  
} else{
  dir.fig <- dir.fig.presentation
  width.single <- 4
  height.single <- 3
  
  width.double <- 2
  height.double <- 2
  
  git.footnote.val <- TRUE
  
  fig.theme <-
    list(theme_bw(), scale_color_manual(values = rhg_cols))
}



# simple function to add footnote
makeFootnote <- function(footnoteText =
                           format(Sys.time(), "%d %b %Y"),
                         size = .3,
                         color = grey(.5))
{
  require(grid)
  pushViewport(viewport())
  grid.text(
    label = footnoteText ,
    x = unit(1, "npc") - unit(1, "mm"),
    y = unit(1.5, "mm"),
    just = c("right", "top"),
    gp = gpar(cex = size, col = color)
  )
  popViewport()
}

## general helper function
to.pdf <-
  function(expr,
           filename,
           ...,
           dir = dir.fig,
           verbose = TRUE,
           git.footnote = TRUE) {
    if (verbose)
      cat(sprintf("Creating %s\n", filename))
    pdf(paste0(dir, filename), ...)
    on.exit(dev.off())
    df <- eval.parent(substitute(expr))
    if (git.footnote == TRUE) {
      makeFootnote(df$git[[1]], color = "white")
    }
  }

to.tex <-
  function(expr,
           filename,
           ...,
           dir = dir.fig,
           verbose = TRUE,
           git.footnote = TRUE) {
    if (verbose)
      cat(sprintf("Creating %s\n", filename))
    tikz(paste0(dir, filename), ...)
    on.exit(dev.off())
    df <- eval.parent(substitute(expr))
    if (git.footnote == TRUE) {
      makeFootnote(df$git[[1]], color = "white")
    }
  }

#Extract Legend

fig.legend <- function(plot) {
  legend <- g_legend(plot)
  grid.draw(legend)
}

g_legend <- function(a.gplot) {
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x)
    x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)
}


# figure for wage changes at deciles
fig.deciles <-
  function(df,
           styling,
           ...,
           returnPlot = FALSE,
           legend = FALSE) {
    if (legend == FALSE)
      leg.pos <- "none"
    else
      leg.pos <- "bottom"
    end
    
    plot <-
      ggplot(df, aes(x = decile, y = value * 100)) + geom_col(aes(fill = version), position =
                                                                "dodge") +
      labs(x = "decile of wage distribution", y = "elast. wrt. robots x 100", fill =
             "") +
      styling +
      ylim(-1.5e-1, 0.05) +
      theme(legend.position = c(0.8, 0.3))
    
    print(plot)
    if (returnPlot == TRUE)
      return(plot)
    end
    return(df)
  }
